const fresh = require('fresh');

module.exports = () => (ctx, next) => next().then(() => {
  const s = ctx.status;
  // 2xx or 304 as per rfc2616 14.26
  if ((s >= 200 && s < 300) || s == 304) {
    if (fresh(ctx.header, ctx.response.header)) {
      ctx.status = 304;
      ctx.body = null;
    }
  }
});
